<?php
$page_type = "A";
require_once 'header.php';

if (!empty($_GET["client_add_error"]))
  $msg = "<font color=\"#CC0000\">{$_GET["client_add_error"]}</font>";
else
  $msg = '';

// Reset add new user form when coming from client.php
if (isset($_GET["new"]) and $_GET["new"] == "true") {
  resetForm();
  $_SESSION['sid'] = "";
}

// Input validation error messages
$client_id_error = "";
$password_error = "";
$fname_error = "";
$lname_error = "";
$client_type_error = "";
$student_type_error = "";
$pflag_error = "";

// Initialize form field variables
if (!isset($_SESSION['client_id']))
  $_SESSION['client_id'] = "";
if (!isset($_SESSION['sid']) or empty($_SESSION['sid']))
  $_SESSION['sid'] = "";
if (!isset($_SESSION['password']))
  $_SESSION['password'] = "";
if (!isset($_SESSION['fname']))
  $_SESSION['fname'] = "";
if (!isset($_SESSION['lname']))
  $_SESSION['lname'] = "";
if (!isset($_SESSION['client_type']))
  $_SESSION['client_type'] = "";
if (!isset($_SESSION['dob']))
  $_SESSION['dob'] = "";
if (!isset($_SESSION['street']))
  $_SESSION['street'] = "";
if (!isset($_SESSION['city']))
  $_SESSION['city'] = "";
if (!isset($_SESSION['state']))
  $_SESSION['state'] = "";
if (!isset($_SESSION['zip']))
  $_SESSION['zip'] = "";
if (!isset($_SESSION['student_type']))
  $_SESSION['student_type'] = "";
if (!isset($_SESSION['input_pflag']))
  $_SESSION['input_pflag'] = "";

// Get postback values 
if (!empty($_POST["sid"]))  {
  if (empty($_SESSION['sid']) and $_POST["sid"] != "Auto-assign") {
    $_SESSION['sid'] = $_POST["sid"];
  }
  else {
    $sid = $_POST["sid"];
  }
}
  
if (!empty($_POST["street"])) 
  $_SESSION['street'] = $_POST["street"];
if (!empty($_POST["city"])) 
  $_SESSION['city'] = $_POST["city"];
if (!empty($_POST["state"])) 
  $_SESSION['state'] = $_POST["state"];
if (!empty($_POST["zip"])) 
  $_SESSION['zip'] = $_POST["zip"];
if (!empty($_POST["dob"])) 
  $_SESSION['dob'] = $_POST["dob"];
  
// Reset form fields
if (isset($_POST["Reset"])) {
  resetForm();
}

// Input validation
if (isset($_POST["Add"])) {

  // Validate client id
  if (empty($_POST["client_id"])) {
    $_SESSION['client_id'] = "";
    $client_id_error = '<font size="3" color="#CC0000">*Required, up to 12 characters</font>';
  }
  else {
    $_SESSION['client_id'] = $_POST["client_id"];
  }

  // Validate password
  if (empty($_POST["password"])) {
    $_SESSION['password'] = "";
    $password_error = '<font size="3" color="#CC0000">*Required, up to 12 characters</font>';
  }
  else {
    $_SESSION['password'] = $_POST["password"];
  }

  // Validate first name
  if (empty($_POST["fname"])) {
    $_SESSION['fname'] = "";
    $fname_error = '<font size="3" color="#CC0000">*Required</font>';
  }
  else {
    $_SESSION['fname'] = $_POST["fname"];
  }

  // Validate last name
  if (empty($_POST["lname"])) {
    $_SESSION['lname'] = "";
    $lname_error = '<font size="3" color="#CC0000">*Required</font>';
  }
  else {
    $_SESSION['lname'] = $_POST["lname"];
  }

  // Validate client type
  if (empty($_POST["client_type"])) {
    $_SESSION['client_type'] = "";
    $client_type_error = '<font size="3" color="#CC0000">*Required</font>';
  }
  else {
    $_SESSION['client_type'] = $_POST["client_type"];
  }
  
  // Validate student type
  if (empty($_POST["student_type"])) {
    $_SESSION['student_type'] = "";
    $student_type_error = '<font size="3" color="#CC0000">*Required</font>';
  }
  else {
    $_SESSION['student_type'] = $_POST["student_type"];
  }
  
  // Validate probation status
  if (empty($_POST["input_pflag"])) {
    $_SESSION['input_pflag'] = "";
    $pflag_error = '<font size="3" color="#CC0000">*Required</font>';
  }
  else {
    $_SESSION['input_pflag'] = $_POST["input_pflag"];
  }
}

// Make sure all fields are filled in
if (!empty($_POST["client_id"]) and !empty($_POST["student_type"]) and !empty($_POST["input_pflag"]) and !empty($_POST["password"]) and !empty($_POST["fname"]) and !empty($_POST["lname"]) and !empty($_POST["client_type"])) {

  // Validated, redirect to client_add_response.php
  header("Location:client_add_response.php");
}
else {
  echo "<h2>New User Form</h2>";

  $sid = $_SESSION['sid'];
  
  $fin = ucfirst(substr("{$_SESSION['fname']}", 0, 1));
  $lin = ucfirst(substr("{$_SESSION['lname']}", 0, 1));
  if (!empty($_SESSION['sid'])) {
    $sid = $fin.$lin.$sid;
  }
  else {
    $sid = "Auto-assign";
  }
  
  // Display insertion form
  echo("
       <form method=\"post\" action=\"client_add.php\">
       Client ID: <input type=\"text\" value =\"{$_SESSION['client_id']}\" size=\"20\" maxlength=\"12\" name=\"client_id\">$client_id_error<br/> 
	   SID(read-only): <input type=\"text\" readonly value =\"$sid\" size=\"20\" maxlength=\"10\" name=\"sid\"><br>
       Password: <input type=\"password\" value =\"{$_SESSION['password']}\" size=\"12\" maxlength=\"12\" name=\"password\">$password_error<br/> 
       First Name: <input type=\"text\" value =\"{$_SESSION['fname']}\" size=\"20\" maxlength=\"30\" name=\"fname\">$fname_error<br/>
       Last Name: <input type=\"text\" value =\"{$_SESSION['lname']}\" size=\"20\" maxlength=\"30\" name=\"lname\">$lname_error<br/>
	   DOB(DD-MON-YY): <input type=\"text\" value =\"{$_SESSION['dob']}\" size=\"20\" maxlength=\"9\" name=\"dob\"><br/>
       Street: <input type=\"text\" value =\"{$_SESSION['street']}\" size=\"20\" maxlength=\"40\" name=\"street\"><br>
       City: <input type=\"text\" value =\"{$_SESSION['city']}\" size=\"20\" maxlength=\"32\" name=\"city\"><br>
       State: <input type=\"text\" value =\"{$_SESSION['state']}\" size=\"20\" maxlength=\"32\" name=\"state\"><br>
       Zip: <input type=\"text\" value =\"{$_SESSION['zip']}\" size=\"20\" maxlength=\"12\" name=\"zip\"><br>");

  // Reselect client type dropdownlist after postback
  if ($_SESSION['client_type']=="") {
    $choose_one_select = htmlspecialchars("selected=\"selected\"");
  }
  if ($_SESSION['client_type']=="A") {
    $a_selection = htmlspecialchars("selected=\"selected\"");
  }
  if ($_SESSION['client_type']=="B") {
    $b_selection = htmlspecialchars("selected=\"selected\"");
  }
  if ($_SESSION['client_type']=="S") {
    $s_selection = htmlspecialchars("selected=\"selected\"");
  }

  // Create the dropdown list for the client type
  echo("Client Type:
        <select name=\"client_type\">
        <option value=\"\" $choose_one_select>Choose One</option>
        <option value=\"A\" $a_selection>Administrator</option>
        <option value=\"B\" $b_selection>Student Administrator</option>
        <option value=\"S\" $s_selection>Student</option>	
        </select>$client_type_error");
	 
  // Reselect student type dropdownlist after postback
  if ($_SESSION['student_type']=="") {
    $choose_one_select1 = htmlspecialchars("selected=\"selected\"");
  }

  if ($_SESSION['student_type']=="U") {
    $u_selection = htmlspecialchars("selected=\"selected\"");
  }

  if ($_SESSION['student_type']=="G") {
    $g_selection = htmlspecialchars("selected=\"selected\"");
  }

  // Create the dropdown list for the student type
  echo("<br>Student Type:
        <select name=\"student_type\">
        <option value=\"\" $choose_one_select1>Choose One</option>
        <option value=\"U\" $u_selection>Undergraduate</option>
        <option value=\"G\" $g_selection>Graduate</option>
        </select>$student_type_error");
	  
  // Reselect probation status dropdownlist after postback
  if ($_SESSION['input_pflag']=="") {
    $choose_one_select2 = htmlspecialchars("selected=\"selected\"");
  }

  if ($_SESSION['input_pflag']=="y") {
    $yp_selection = htmlspecialchars("selected=\"selected\"");
  }

  if ($_SESSION['input_pflag']=="n") {
    $np_selection = htmlspecialchars("selected=\"selected\"");
  }

  // Create the dropdown list for probation status
  echo("<br>Probation Status:
        <select name=\"input_pflag\">
        <option value=\"\" $choose_one_select2>Choose One</option>
        <option value=\"y\" $yp_selection>Yes</option>
        <option value=\"n\" $np_selection>No</option>
        </select>$pflag_error
	  
	    <br><input type=\"submit\" name=\"Add\" value=\"Add\">
	    <input type=\"submit\" name=\"Reset\" value=\"Reset\">
        </form>
	    <form method=\"post\" action=\"client.php\">
        <input type=\"submit\" value=\"Go Back\">
        </form>$msg"); 
}
?>